import 'dart:convert';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart' hide Page;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
import '../provider/person_info.dart';
import 'package:http/http.dart' as http;

import '../tool/toolweb.dart';

class DelAct extends StatefulWidget {
  const DelAct({super.key});

  @override
  State<StatefulWidget> createState() => _DelAct();
}

class _DelAct extends State<DelAct> {
  List delAct = [];
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      final personInfo = Provider.of<PersonInfo>(context, listen: false);
      var url = Uri.parse('http://127.0.0.1:5000/delact_show');
      var headers = {'Content-Type': 'application/json;charset=utf-8'};
      var body = jsonEncode({'alliance': personInfo.alliance});
      final request = http.Request('get', url);
      request.headers.addAll(headers);
      request.body = body;
      request.send().then((value) {
        http.Response.fromStream(value).then((response) {
          setState(() {
            delAct = jsonDecode(response.body)["msg"];
          });
        });
      }).catchError((value) {
        Fluttertoast.showToast(msg: '初始化异常');
      });
    });
  }

  void deleteAct(PersonInfo person, String act) async {
    await showDialog<String>(
        context: context,
        builder: (context) => Container(
              margin: const EdgeInsets.only(left: 18, right: 18),
              child: fluent.ContentDialog(
                title: const Text('删除行动'),
                content: const Text(
                  '是否需要删除行动',
                ),
                actions: [
                  fluent.Button(
                    child: const Text('取消'),
                    onPressed: () async {
                      Navigator.pop(context, '取消');
                      // Delete file here
                    },
                  ),
                  FilledButton(
                    child: const Text('确认'),
                    onPressed: () async {
                      final url =
                          Uri.parse('http://127.0.0.1:5000/delact_agree');
                      final headers = {
                        'Content-Type': 'application/json;charset=utf-8'
                      };
                      final body =
                          jsonEncode({'alliance': person.alliance, 'act': act});
                      var response =
                          await http.post(url, headers: headers, body: body);
                      if (response.statusCode == 200) {
                        Fluttertoast.showToast(msg: '修改成功');
                        delAct.remove(act);
                        if (act == person.act) {
                          person.changAct(value: delAct[delAct.length - 1]);
                        }
                        setState(() {});
                      } else {
                        Fluttertoast.showToast(msg: '修改失败');
                      }
                      // ignore: use_build_context_synchronously
                      Navigator.pop(context, '确认');
                    },
                  ),
                ],
              ),
            ));
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Toolweb(
        name: "删除行动", list: delAct, agree: deleteAct, agreeName: "删除");
  }
}
